﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DataAccessLayer;
using DataAccessLayer.DAL;
using Common;

namespace BestBuy.UserViews
{
    public partial class PostNewListing : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            User user = UserDAL.GetUserByUsername(HttpContext.Current.User.Identity.Name);
            if (user == null)
            {
                Response.Redirect("~/Account/Login.aspx");
            }
        }

        protected void PostNewListingWizard_NextButtonClick(object sender, WizardNavigationEventArgs e)
        {
            if (PostNewListingWizard.ActiveStep == SelectCategoryStep)
            {
                    Page.Validate("SelectCategoryValidators");
            }
            if (PostNewListingWizard.ActiveStep == ProductDetailsStep)
            {
                Page.Validate("ProductDetailValidations");
            }

            if (!IsValid)
            {
                e.Cancel = true;
                return;
            }
        }

        protected void PostNewListingWizard_ActiveStepChanged(object sender, EventArgs e)
        {
            if (PostNewListingWizard.ActiveStep == SelectCategoryStep)
            {
                var nextButton = PostNewListingWizard.FindControl("StepNextButton") as Button;
                if (nextButton != null)
                {
                    nextButton.ValidationGroup = "SelectCategoryValidators";
                }
            }
            if (PostNewListingWizard.ActiveStep == ProductDetailsStep)
            {
                var nextButton = PostNewListingWizard.FindControl("StepNextButton") as Button;
                if (nextButton != null)
                {
                    nextButton.ValidationGroup = "ProductDetailValidations";
                }
                int? productID = SelectCategoryControl.SelectedSubCategoryID;
                if (productID != null)
                {
                    ProductDetailControl.LoadInfo((int)productID);
                }
            }
            if (PostNewListingWizard.ActiveStep == PreviewStep)
            {
                LoadPreviewProduct(ProductDetailControl, PreviewProductControl);
            }
        }

        protected void SideBar_ItemDataBound(object sender, ListViewItemEventArgs e)
        {
            LinkButton button = e.Item.FindControl("sideBarButton") as LinkButton;
            if (button != null)
            {
                if (e.Item.DisplayIndex <= PostNewListingWizard.ActiveStepIndex)
                {
                    button.Enabled = true;
                    button.CssClass = "enabled";
                }
                else
                {
                    button.Enabled = false;
                    button.CssClass = "disabled";
                }
                
            }
        }

        private void LoadPreviewProduct(UserControls.ProductDetail detail, UserControls.PreviewProduct preview)
        {
            preview.AllowOthersSellerJoin = detail.AllowOthersSellerJoin;
            preview.AuctionType = detail.AuctionType;
            preview.Title = detail.Title;
            preview.SubTitle = detail.SubTitle;
            preview.Description = detail.Description;
            preview.Quantity = detail.Quantity;
            preview.StartPrice = detail.StartPrice;
            preview.MaxPrice = detail.MaxPrice;
            preview.StartTime = detail.StartTime;
            preview.EndTime = detail.EndTime;
            preview.ImageUrl = detail.ImageUrl;
            preview.Payment = detail.Payment;
            preview.FreeShipping = detail.FreeShipping;
            preview.ProductID = detail.ProdictID;
            preview.SellerList = detail.SellerList;
        }

        protected void PostNewListingWizard_FinishButtonClick(object sender, WizardNavigationEventArgs e)
        {
            if (!IsValid) 
            {
                e.Cancel = true;
                return;
            }
            CreateAuction(PreviewProductControl);
            Response.Redirect("~/BuyerViews/ManagerAuctions.aspx");
        }

        private void CreateAuction(UserControls.PreviewProduct pre)
        {
            //insert table : sellerNotified,auction,AuctionPaymentType
            Auction auc = InitAuction(pre);
            Dictionary<string, int> sellerlist = pre.SellerList;
            Dictionary<string, int> paymentlist = pre.Payment;
            AuctionDAL.CreateAuction(auc, sellerlist, paymentlist);
        }

        private static Auction InitAuction(UserControls.PreviewProduct pre)
        {
            Auction auc = new Auction();
            auc.CreateByUserID = UserDAL.GetUserIDByUsername(HttpContext.Current.User.Identity.Name);
            auc.CategoryID = pre.ProductID;
            auc.AuthorizeSeller = pre.AllowOthersSellerJoin;
            auc.Title = pre.Title;
            auc.Subtile = pre.SubTitle;
            auc.Description = pre.Description;
            //auc.ImageData = pre.ImageUrl;
            auc.Quantity = pre.Quantity;
            auc.StartPrice = pre.StartPrice;
            auc.IsCloseAuction = pre.AuctionType;
            auc.StartTime = pre.StartTime;
            auc.EndTime = pre.EndTime;
            auc.CreatedDate = DateTime.Now;
            auc.Status = (int)AuctionStatus.Watting;
            return auc;
        }
    }
}